package com.xckj.teacher.presenter

import com.blankj.utilcode.util.ToastUtils
import com.xckj.teacher.bean.BaseBean
import com.xckj.teacher.bean.UserInfoBean
import com.xckj.teacher.framework.IBaseView
import com.xckj.teacher.framework.net.RetrofitManager
import com.xckj.teacher.global.Constant
import com.xckj.teacher.ui.appViewModel
import com.xckj.teacher.util.CacheUtil
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.Objects

class MainPresenter(private val iBaseView: IBaseView) {

    fun getUserInfo() {
        iBaseView.showLoading()
        val call = RetrofitManager.getInstance().service.getUserInfo()
        call.enqueue(object : Callback<BaseBean<UserInfoBean?>> {
            override fun onResponse(
                call: Call<BaseBean<UserInfoBean?>>,
                response: Response<BaseBean<UserInfoBean?>>
            ) {
                iBaseView.hideLoading()
                if (response.isSuccessful) {
                    val baseBean = response.body()!!
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        CacheUtil.setUser(baseBean.data)
                        appViewModel.userInfoData.value = baseBean.data
                    } else {
                        ToastUtils.showShort(baseBean.msg)
                    }
                } else {
                    ToastUtils.showShort("请求失败")
                }
            }

            override fun onFailure(call: Call<BaseBean<UserInfoBean?>>, t: Throwable) {
                t.printStackTrace()
                iBaseView.hideLoading()
                ToastUtils.showShort("请求失败${t.message}")
            }
        })
    }


}
